package com.sky.myapplication.kotlin.kotlinActivity

import android.os.Bundle
import android.text.InputType
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.sky.myapplication.R
import com.sky.myapplication.kotlin.kotlinAdapter.FragmentAdapter
import com.sky.myapplication.kotlin.kotlinFragment.OneFragment
import com.sky.myapplication.kotlin.kotlinFragment.ThreeFragment
import com.sky.myapplication.kotlin.kotlinFragment.TwoFragment

/**
 * Created by Min
 * Time 2022/10/19
 * Desc
 */
class FragmentViewPageActivity : AppCompatActivity() {
    lateinit var et_text : EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_fragment_view_page)
//
//        val framentList = ArrayList<Fragment>();
//        val tv_one = findViewById<TextView>(R.id.tv_one)
//        val tv_two = findViewById<TextView>(R.id.tv_two)
//        val tv_three = findViewById<TextView>(R.id.tv_three)
//        val vp_body = findViewById<ViewPager>(R.id.vp_body)
//
//        et_text = findViewById<EditText>(R.id.et_text)
////        et_text.setOnTouchListener(View.OnTouchListener { v, event ->
////            val inType: Int = et_text.getInputType() // backup the input type
////            et_text.setInputType(InputType.TYPE_NULL) // disable soft input
////            et_text.onTouchEvent(event) // call native handler
////            et_text.setInputType(inType) // restore input type
////            true
////        })
//
//        val oneFragment = OneFragment()
//        val twoFragment = TwoFragment()
//        val threeFragment = ThreeFragment()
//
//        framentList.add(oneFragment);
//        framentList.add(twoFragment);
//        framentList.add(threeFragment);
//
//        var manager = supportFragmentManager
//        val fragmentAdapter = FragmentAdapter(manager)
//        fragmentAdapter.setList(framentList)
//        vp_body.adapter = fragmentAdapter
//        vp_body.offscreenPageLimit = 3
//
//        tv_one.setOnClickListener {
//            vp_body.currentItem = 0;
//        }
//
//        tv_two.setOnClickListener {
//            vp_body.currentItem = 1;
//        }
//
//        tv_three.setOnClickListener {
//            vp_body.currentItem = 2;
//        }
//
////        var transaction = manager.beginTransaction();
////
////        transaction.add(R.id.fl_body, twoFragment)
////        transaction.add(R.id.fl_body, threeFragment)
////        transaction.add(R.id.fl_body, oneFragment)
////        transaction.commit()


    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (ev.action == MotionEvent.ACTION_DOWN) {
            val v = currentFocus
            if (isShouldHideInput(v, ev)) { //点击editText控件外部
                val imm = this.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                if (imm != null) {
                    assert(v != null)
                    closeKeyboard(et_text) //软键盘工具类
                    if (et_text != null) {
                        et_text.clearFocus()
                    }
                }
            }
            return super.dispatchTouchEvent(ev)
        }
        //必不可少，否则所有的组件都不会有TouchEvent了
        return window.superDispatchTouchEvent(ev) || onTouchEvent(ev)
    }


    fun isShouldHideInput(v: View?, event: MotionEvent): Boolean {
        if (v != null && v is EditText) {
            et_text = v
            val leftTop = intArrayOf(0, 0)
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop)
            val left = leftTop[0]
            val top = leftTop[1]
            val bottom = top + v.getHeight()
            val right = left + v.getWidth()
            return !(event.x > left && event.x < right && event.y > top && event.y < bottom)
        }
        return false
    }

    fun closeKeyboard(view: View) {
        val imm = view.context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }
}